package com.itboye.lottery.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.itboye.lottery.R;
import com.itboye.lottery.base.BaseActivity;
import com.txby.zxing.utils.CodeUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @author
 * @date 2018/7/23
 * @description 生成二维码
 */
public class CodeGenerrateActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_code;
    private String url;
    private String path;

    private void generateCodeImage() {
        Bitmap bitmap = CodeUtils.createImage(url, 400, 400, null);//生成普通的二维码图片
//        Bitmap bitmap = CodeUtils.createImage(codeContent, 400, 400,
//                            BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));//生成带Logo的二维码
        iv_code.setImageBitmap(bitmap);
        path = saveBitmapToSD(bitmap, new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(System.currentTimeMillis()));
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.setMessage("图片已保存至:" + path + "中,请打开微信扫一扫进行支付");
        alertDialog.setPositiveButton("确定", null);
        alertDialog.show();
    }


    //将Bitmap图片保存到sd卡

    protected String saveBitmapToSD(Bitmap bt, String imagename) {
        String sdCardDir = Environment.getExternalStorageDirectory() + "/lotteryQr/";
        File dirFile = new File(sdCardDir);  //目录转化成文件夹
        if (!dirFile.exists()) {                //如果不存在，那就建立这个文件夹
            dirFile.mkdirs();
        }
        File file = new File(sdCardDir, imagename + ".jpg");
        System.out.println(file.getPath());
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
            bt.compress(Bitmap.CompressFormat.JPEG, 90, out);
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//如果是4.4及以上版本
            Intent mediaScanIntent = new Intent(
                    Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri contentUri = Uri.fromFile(file); //out is your output file
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent);
        } else {
            sendBroadcast(new Intent(
                    Intent.ACTION_MEDIA_MOUNTED,
                    Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));
        }
        return file.getPath();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.btn_next:
//                startActivity(new Intent(this, BindResultActivity.class).putExtra("device", deviceType).putExtra("aq_did", aq_did));
//                break;
//            case R.id.img_back:
//                finish();
//                break;
//            case R.id.iv_code:
//                ImageViewerDialog imageViewerDialog = new ImageViewerDialog(
//                        this);
//                String sdPath = "file://" + path;
//                System.out.println("path" + sdPath);
//                String[] urls = {sdPath};
//                imageViewerDialog.setImageUrls(urls);
//                imageViewerDialog.show(0);
//                break;
            default:
                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_code_generate;
    }

    @Override
    protected void initData() {
        url = getIntent().getStringExtra("url");
        generateCodeImage();
        Log.v("scan_code", url);
    }
}
